package cn.kgc.spring06.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/4/29
 * @Description:
 */
@Component
@Aspect  //标识当前类是通知类
public class AllAdvice {


    //@Pointcut("execution(String cn.kgc.spring06.interfac.impl.TeacherWang.*(String))")
    @Pointcut("@annotation(cn.kgc.spring06.anno.Log)")
    public void  point(){}

    // 前置通知
    @Before("point()")
    public void  beforeAdvice(){
        System.out.println("----------上课时间到了---------------");
    }

    // 后置通知     目标方法正确执行
    @AfterReturning("point()")
    public void  afterReturnAdvice(){
        System.out.println("----------下课时间到了---------------");
    }

    //@After("point()")
    public void afterAdvice(){

        System.out.println("----------最终通知---------------");
    }

    // 异常通知    目标方法出现异常时执行
    //@AfterThrowing("point()")
    public void afterThrowingAdvice(){
        System.out.println("----------异常通知---------------");
    }


    //环绕通知
    //@Around("point()")
    public Object aroundAdvice(ProceedingJoinPoint  joinPoint){


        // 目标方法执行
        try {
            System.out.println("----------环绕前通知---------------");

            Object proceed = joinPoint.proceed();

            System.out.println("----------环绕后通知---------------");
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }


        return null;
    }



}
